#include "yscamera.h"
#include <curl/curl.h>
#include "common.h"
#include "log.h"
#include "config.h"
#include "../cJSON/cJSON.h"
#include <stdio.h>

#define USERNAME "admin"
#define PASSWORD "bjhg@123456"

void reset_snapshot_config(uint8_t *ip)
{
    CURL *curl;
    char url[128] = {0};
    char *json_str = NULL;
    char ip_str[16];
    CURLcode res;
    snprintf(ip_str, sizeof(ip_str), "%u.%u.%u.%u", (unsigned char)ip[0], (unsigned char)ip[1], (unsigned char)ip[2], (unsigned char)ip[3]);
    snprintf(url, 128, "http://%s/LAPI/V1.0/Channels/0/Media/Snapshot/Info", ip_str);
    curl = curl_easy_init();
    if (!curl) {
        printf("error: unable to initialize curl\n");
        return;
    }
    cJSON *json = cJSON_CreateObject();
    cJSON_AddNumberToObject(json, "Enabled", 1);
    json_str = cJSON_Print(json);

    curl_easy_setopt(curl, CURLOPT_URL, url);
    curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PUT");
    curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
    curl_easy_setopt(curl, CURLOPT_USERNAME, USERNAME);
    curl_easy_setopt(curl, CURLOPT_PASSWORD, PASSWORD);
    curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, json_str);
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, NULL);
    curl_easy_setopt(curl, CURLOPT_TIMEOUT, 5L);
    curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 1L);
    res = curl_easy_perform(curl);

    free(json_str);
    if (res != CURLE_OK) {
        printf("error: curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
    }

    curl_easy_cleanup(curl);
    cJSON_Delete(json);
}

bool ys_snapshot(const char *path, char *ip)
{
    CURL *curl;
    FILE *output;
    CURLcode res;
    char ip_str[16];
    snprintf(ip_str, sizeof(ip_str), "%u.%u.%u.%u", (unsigned char)ip[0], (unsigned char)ip[1], (unsigned char)ip[2], (unsigned char)ip[3]);

    char url[128];
    snprintf(url, sizeof(url), "http://%s/LAPI/V1.0/Channels/0/Media/Video/Streams/0/Snapshot", ip_str);

    curl = curl_easy_init();
    if (!curl) {
        printf("error: unable to initialize curl\n");
        return false;
    }

    output = fopen(path, "wb");
    if (!output) {
        printf("error: unable to open file %s\n", path);
        curl_easy_cleanup(curl);
        return false;
    }

    curl_easy_setopt(curl, CURLOPT_URL, url);
    curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
    curl_easy_setopt(curl, CURLOPT_USERNAME, USERNAME);
    curl_easy_setopt(curl, CURLOPT_PASSWORD, PASSWORD);
    curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, NULL);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, output);
    curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10L);
    curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 5L);

    res = curl_easy_perform(curl);

    fclose(output);
    curl_easy_cleanup(curl);

    if (res != CURLE_OK) {
        printf("error: curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
        if (res == CURLE_OPERATION_TIMEDOUT) {
            reset_snapshot_config(ip);
        }
        return false;
    }
    return true;
}